﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace AppDesigner.Helper
{
    public static class StringExtensions
    {
        public static Thickness ToMargin(this string marginStr) 
        {
            Thickness margin = new Thickness();

            string[] marginStrArray = marginStr.Split(',');

            if (marginStrArray.Length == 1)
            {
                margin.Left = margin.Right = margin.Top = margin.Bottom = Convert.ToDouble(marginStrArray[0]);
            }
            else if (marginStrArray.Length == 2)
            {
                margin.Left = margin.Right = Convert.ToDouble(marginStrArray[0]);
                margin.Top = margin.Bottom = Convert.ToDouble(marginStrArray[1]);
            }
            else if (marginStrArray.Length == 4)
            {
                margin.Left =  Convert.ToDouble(marginStrArray[0]);
                margin.Right = Convert.ToDouble(marginStrArray[1]);
                margin.Top = margin.Bottom = Convert.ToDouble(marginStrArray[2]);
                margin.Bottom = Convert.ToDouble(marginStrArray[3]);
            }
            return margin;
        }

        public static HorizontalAlignment GetHorizontalAlignment(this string alignmentStr)
        {
            string[] strArray = alignmentStr.Split(',');

            if ("L".Equals(strArray[0]))
            {
                return HorizontalAlignment.Left;
            }
            else if ("R".Equals(strArray[0]))
            {
                return HorizontalAlignment.Right;
            }
            else if ("C".Equals(strArray[0]))
            {
                return HorizontalAlignment.Center;
            }
            else
            {
                return HorizontalAlignment.Center;
            }
        }

        public static VerticalAlignment GetVerticalAlignment(this string alignmentStr)
        {
            string[] strArray = alignmentStr.Split(',');

            if ("T".Equals(strArray[1]))
            {
                return VerticalAlignment.Top;
            }
            else if ("B".Equals(strArray[1]))
            {
                return VerticalAlignment.Bottom;
            }
            else if ("C".Equals(strArray[1]))
            {
                return VerticalAlignment.Center;
            }
            else
            {
                return VerticalAlignment.Center;
            }
        }

        public static Orientation ToOrientation(this string orientationStr)
        {
            if ("H".Equals(orientationStr))
            {
                return Orientation.Horizontal;
            }
            else if ("V".Equals(orientationStr))
            {
                return Orientation.Vertical;
            }
            else
            {
                return Orientation.Vertical;
            }
        }
    }
}
